using CookComputing.MetaWeblog;
using System.Net;

namespace CookComputing.XmlRpc.MetaBlogAPI
{
    [XmlRpcUrl("http://plant.blogger.com/api/RPC2")]
    public class MetaBlogAPI : XmlRpcClientProtocol
    {
        [XmlRpcMethod("metaWeblog.editPost")]
        public bool EditPost(string appkey, string postid, string username, string password, string contents, bool publish)
        {
            try
            {
                return (bool) Invoke("EditPost", new object[] { appkey, postid, username, password, contents, publish });
            }
            catch
            {
                return false;
            }
        }

        [XmlRpcMethod("metaWeblog.getCategories")]
        public CategoryInfo[] GetCategories(string blogid, string username, string password)
        {
            try
            {
                return (CategoryInfo[]) Invoke("GetCategories", new object[] { blogid, username, password });
            }
            catch (WebException)
            {
                var info = new CategoryInfo {title = "Error Connecting to Web. You must be online to get categories."};
                return new[] { info };
            }
            catch (XmlRpcException exception)
            {
                var info2 = new CategoryInfo {title = "Error Retrieving Categories" + exception.Message};
                return new[] { info2 };
            }
            catch (XmlRpcFaultException exception2)
            {
                var info3 = new CategoryInfo {title = "Error Retrieving Categories" + exception2.Message};
                return new[] { info3 };
            }
        }

        [XmlRpcMethod("metaWeblog.getRecentPosts")]
        public RecentPost[] GetRecentPosts(string blogid, string username, string password, int numberOfPosts)
        {
            return (RecentPost[]) Invoke("GetRecentPosts", new object[] { blogid, username, password, numberOfPosts });
        }

        [XmlRpcMethod("metaWeblog.getTemplate")]
        public string GetTemplate(string appkey, string blogid, string username, string password, string templateType)
        {
            return (string) Invoke("GetTemplate", new object[] { appkey, blogid, username, password, templateType });
        }

        [XmlRpcMethod("metaWeblog.getUserInfo")]
        public UserInfo GetUserInfo(string appkey, string username, string password)
        {
            return (UserInfo) Invoke("GetUserInfo", new object[] { appkey, username, password });
        }

        [XmlRpcMethod("metaWeblog.getUsersBlogs")]
        public UserBlog[] GetUsersBlogs(string appkey, string username, string password)
        {
            return (UserBlog[]) Invoke("GetUsersBlogs", new object[] { appkey, username, password });
        }

        [XmlRpcMethod("metaWeblog.newPost")]
        public string newPost(string blogid, string blogUserName, string blogPW, CKS.EBE.API.MetaWeblog.Post post, bool publish)
        {
            try
            {
                return (string) Invoke("newPost", new object[] { blogid, blogUserName, blogPW, post, publish });
            }
            catch (XmlRpcException exception)
            {
                return exception.ToString();
            }
            catch (XmlRpcFaultException exception2)
            {
                return exception2.ToString();
            }
        }

        [XmlRpcMethod("metaWeblog.setTemplate")]
        public string SetTemplate(string appkey, string blogid, string username, string password, string template, string templateType)
        {
            return (string) Invoke("SetTemplate", new object[] { appkey, blogid, username, password, template, templateType });
        }
    }
}